﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using HadronData;

namespace Hadron
{
    class GameStateMission
        : GameState
    {
        private enum MissionState
        {
            FadeIn,
            Running,
            FadeOut
        }

        private static GameState s_gameState = new GameStateMission();

        private MissionState m_missionState;
        private float m_fStateTimer;
        private bool m_bCancel;
        private Texture2D m_textureTitleBackground;
        private Texture2D m_textureGamepadStart;
        private Texture2D m_textureGamepadBack;
        private Texture2D m_textureGamepadUp;
        private Texture2D m_textureGamepadLeft;
        private Texture2D m_textureGamepadRight;
        private Texture2D m_textureGamepadA;
        private Texture2D m_textureGamepadB;
        private GameFont m_gameFont;

        public static GameState Instance
        {
            get { return s_gameState; }
        }

        private GameStateMission()
        {
        }

        public override void OnEnter()
        {
            ContentManager contentManager = m_gameContext.Game.Content;

            m_textureTitleBackground = contentManager.Load<Texture2D>("Graphics\\MissionBackground");
            m_textureGamepadStart = contentManager.Load<Texture2D>("Graphics\\GamepadStart");
            m_textureGamepadBack = contentManager.Load<Texture2D>("Graphics\\GamepadBack");
            m_textureGamepadUp = contentManager.Load<Texture2D>("Graphics\\GamepadUp");
            m_textureGamepadLeft = contentManager.Load<Texture2D>("Graphics\\GamepadLeft");
            m_textureGamepadRight = contentManager.Load<Texture2D>("Graphics\\GamepadRight");
            m_textureGamepadA = contentManager.Load<Texture2D>("Graphics\\GamepadA");
            m_textureGamepadB = contentManager.Load<Texture2D>("Graphics\\GamepadB");
            m_gameFont = new GameFont(AssetManager.Fonts.Title);

            m_missionState = MissionState.FadeIn;
            m_fStateTimer = 0.5f;

            // store progress in save game
            SaveGame saveGame = new SaveGame();
            saveGame.Level = m_gameContext.CurrentLevel;
            saveGame.Score = m_gameContext.Score;
            saveGame.Save(m_gameContext.StorageDevice);
        }

        public override void OnExit()
        {
            m_gameFont = null;

            m_textureGamepadB = null;
            m_textureGamepadA = null;
            m_textureGamepadRight = null;
            m_textureGamepadLeft = null;
            m_textureGamepadUp = null;
            m_textureGamepadBack = null;
            m_textureGamepadStart = null;
            m_textureTitleBackground = null;
        }

        public override void Update(GameTime p_gameTime)
        {
            float fDeltaTime = (float)p_gameTime.ElapsedGameTime.TotalSeconds;

            if (m_missionState == MissionState.Running &&
                (m_gameController.Start || m_gameController.Back))
            {
                m_fStateTimer = 0.5f;
                m_missionState = MissionState.FadeOut;
                m_bCancel = m_gameController.Back;
                AssetManager.Sounds.MenuItem.Play();
            }

            m_fStateTimer = Math.Max(0.0f, m_fStateTimer - fDeltaTime);

            if (m_fStateTimer == 0.0f)
            {
                switch (m_missionState)
                {
                    case MissionState.FadeIn: m_missionState = MissionState.Running; break;
                    case MissionState.FadeOut: NextState = m_bCancel ? GameStateTitle.Instance : GameStatePlay.Instance; break;
                }
            }
        }

        public override void Draw(SpriteBatch p_spriteBatch)
        {
            p_spriteBatch.Begin();
            p_spriteBatch.Draw(m_textureTitleBackground, Vector2.Zero, Color.White);

            m_gameFont.Colour = Color.YellowGreen;
            m_gameFont.DrawString(p_spriteBatch, new Vector2(75.0f, 180.0f),
                "MISSION " + (m_gameContext.CurrentLevel + 1) + " - BRIEFING");

            // mission text
            m_gameFont.Colour = Color.LightGray;
            Vector2 vecItemPosition = new Vector2(75.0f, 200.0f);
            Level level = m_gameContext.World.Levels[m_gameContext.CurrentLevel];
            String[] arrayLines = level.Mission;
            /*for (int nIndex = 0; nIndex < arrayLines.Length; nIndex++)
            {
                m_gameFont.DrawString(p_spriteBatch, vecItemPosition, arrayLines[nIndex]);
                vecItemPosition.Y += 16.0f;
            }*/
            foreach (String strLine in level.Mission)
            {
                vecItemPosition = m_gameFont.DrawString(p_spriteBatch, vecItemPosition, 500.0f, strLine);
                vecItemPosition.X = 75.0f;
            }

            float fFadeOpacity = 0.0f;
            switch (m_missionState)
            {
                case MissionState.FadeIn: fFadeOpacity = m_fStateTimer * 2.0f; break;
                case MissionState.Running: fFadeOpacity = 0.0f; break;
                case MissionState.FadeOut: fFadeOpacity = 1.0f - m_fStateTimer * 2.0f; break;
            }
            
            // game buttons
            m_gameFont.Colour = Color.YellowGreen;
            m_gameFont.DrawString(p_spriteBatch, new Vector2(75.0f, 30.0f), "Ship Controls");

            m_gameFont.Colour = Color.LightGray;
            p_spriteBatch.Draw(m_textureGamepadUp, new Vector2(75.0f, 60.0f), Color.White);
            m_gameFont.DrawString(p_spriteBatch, new Vector2(120.0f, 68.0f), "Forward thruster");
            p_spriteBatch.Draw(m_textureGamepadLeft, new Vector2(340.0f, 60.0f), Color.White);
            m_gameFont.DrawString(p_spriteBatch, new Vector2(380.0f, 68.0f), "Rotate left");
            p_spriteBatch.Draw(m_textureGamepadRight, new Vector2(600.0f, 60.0f), Color.White);
            m_gameFont.DrawString(p_spriteBatch, new Vector2(640.0f, 68.0f), "Rotate right");
            p_spriteBatch.Draw(m_textureGamepadA, new Vector2(75.0f, 100.0f), Color.White);
            m_gameFont.DrawString(p_spriteBatch, new Vector2(120.0f, 108.0f), "Fire weapon");
            p_spriteBatch.Draw(m_textureGamepadB, new Vector2(340.0f, 100.0f), Color.White);
            m_gameFont.DrawString(p_spriteBatch, new Vector2(380.0f, 108.0f), "Activate shield");

            // mission buttons
            p_spriteBatch.Draw(m_textureGamepadStart, new Vector2(100.0f, 500.0f), Color.White);
            m_gameFont.DrawString(p_spriteBatch, new Vector2(140.0f, 508.0f), "Start mission");
            p_spriteBatch.Draw(m_textureGamepadBack, new Vector2(600.0f, 500.0f), Color.White);
            m_gameFont.DrawString(p_spriteBatch, new Vector2(640.0f, 508.0f), "Back");

            p_spriteBatch.End();

            // screen flicker
            p_spriteBatch.Begin(SpriteBlendMode.Additive);
            Random random = new Random();
            float fY = 200.0f + (float)random.NextDouble() * 200.0f;
            Edge edge = new Edge(585.0f, fY, 748.0f, fY, new Color(0, 32, 0));
            edge.Draw(p_spriteBatch);
            p_spriteBatch.End();

            // fade in/out
            p_spriteBatch.Begin();
            p_spriteBatch.Draw(AssetManager.Textures.Fade,
                new Rectangle(0, 0, 800, 600),
                new Color(Color.Black, fFadeOpacity));
            p_spriteBatch.End();

        }
    }
}
